<template>
  <div :class="{'indexTable bgWhite productComment expandTable': true, 'tableExpand': !isExpand}" ref="content">
   <el-form ref="form" class="pb_form ">
     <el-select  v-model="searchForm.product_model_name.value" placeholder="产品型号名称" filterable clearable 
         @keyup.enter.native="getTableList" @focus="getProductModelNameList" >
        <el-option v-for="item in productModelNameList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
     <el-select  v-model="searchForm.site.value" placeholder="站点"  clearable  @keyup.enter.native="getTableList" @focus="getSiteList" >
        <el-option v-for="item in siteList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-select  v-model="searchForm.product_name.value" placeholder="产品编码" filterable clearable  
         @keyup.enter.native="getTableList" @focus="getProductNameList" >
        <el-option v-for="item in productNameList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-input v-model="searchForm.asin.value " placeholder="ASIN" @keyup.enter.native="getTableList"></el-input>
      <el-select  v-model="searchForm.first_type.value" placeholder="一级类型" clearable filterable  
        @keyup.enter.native="getTableList" @focus="getFirstTypeList" @clear="clearFirstType">
        <el-option v-for="item in firstTypeList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-select  v-model="searchForm.second_type.value" placeholder="二级类型" clearable  
        @keyup.enter.native="getTableList"  @focus="getSecondTypeList" >
        <el-option v-for="item in secondTypeList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>

      <el-input v-model="searchForm.comment_username.value " placeholder="评论用户名" @keyup.enter.native="getTableList"></el-input>
      <div class="createTime">
         <el-input v-model="commentTime" placeholder="评论日期"></el-input>
         <el-date-picker @change="changeCommentTime"   v-model="commentTimePicker"
          type="datetimerange" range-separator="至"  :default-time="['00:00:00' , '23:59:59']" 
         start-placeholder="开始日期"  value-format="yyyy-MM-dd HH:mm:ss" end-placeholder="结束日期" placeholder="选择操作时间"></el-date-picker>
      </div>
      <el-select  v-model="searchForm.comment_star.value" placeholder="评论星级" clearable  @keyup.enter.native="getTableList"  >
        <el-option v-for="item in commentStarList" :key="item.label" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-select  v-model="searchForm.comment_sort.value" placeholder="评论排序" clearable  @keyup.enter.native="getTableList"  >
        <el-option v-for="item in commentSortList" :key="item.label" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-input v-model="searchForm.create_name.value " placeholder="创建人" @keyup.enter.native="getTableList"></el-input>
      <div class="isExpand" @click="isExpandForm">
        <p class="hide" v-show="isExpand">收起 <i class="iconfont icon-arrow_1"></i></p>
        <p class="expand" v-show="!isExpand">展开 <i class="iconfont icon-arrow_1"></i></p>
      </div>
    </el-form>
    <div class="btnContainer" ref="btn">
      <div> 
        <el-button type="search" @click="closeAddDialog" v-if="$_has('/product_comment/add')">添加</el-button>
        <el-button type="del" @click="sureCloseDialog('sort')" v-if="$_has('/product_comment/set_sort')">排序</el-button>
        <el-button type="del" @click="sureCloseDialog('del')" v-if="$_has('/product_comment/del')">删除</el-button>
        <el-button type="del" @click="closeExportDialog()" v-if="$_has('/product_comment/export')">导出</el-button>
      </div>
      <div>
        <el-button type="search" @click="getTableList" v-if="$_has('/product_comment/list')">查询</el-button>
        <el-button type="del" @click="resetForm">重置</el-button>
      </div>
    </div>
    <el-table :data="listTable" :height="tableH" :row-class-name="rowClassName" ref="table" @row-click="RowClick"
     @selection-change="handleSelectionChange" :row-key="function(row){return row.id}" v-loading="loading">
      <el-table-column type="selection" width="64"  fixed="left" :reserve-selection="true"></el-table-column>
      <el-table-column  label="产品型号名称" width="208" prop="product_model_name" align="left"></el-table-column>
      <el-table-column  label="站点"    prop="site" ></el-table-column>
      <el-table-column  label="产品编码" width="120"  prop="product_name" align="left"> </el-table-column>
      <el-table-column  label="ASIN"  width="160" prop="asin" align="left"></el-table-column>
      <el-table-column  label="一级类型"  width="142"   prop="first_type"  align="left"> </el-table-column>
      <el-table-column  label="二级类型"  width="142"  prop="second_type" align="left"></el-table-column>
      <el-table-column  label="评论ID"  width="160"   prop="comment_id" align="left"></el-table-column>
      <el-table-column  label="评论用户名"  width="144"   prop="comment_username" align="left"></el-table-column>
      <el-table-column  label="评论日期" width="128"  prop="comment_time" > </el-table-column>
      <el-table-column  label="评论标题"  width="400" prop="comment_title" align="left"> </el-table-column>
      <el-table-column  label="评论内容"  width="400"   prop="comment_content" align="left"></el-table-column>
      <el-table-column  label="产品属性" width="160"  prop="attributes" align="left"> </el-table-column>
      <el-table-column  label="Amazon评论链接"  width="400" prop="amazon_review_link" align="left"></el-table-column>
      <el-table-column  label="评论图片"  width="360"   prop="comment_image" align="left">
        <template slot-scope="scope" >
        <div  class="comment_image">
          <el-image v-for="item in scope.row.httpImage" :key="item"  :preview-src-list="scope.row.httpImage"
          :src="item"  ></el-image>
        </div>
        
        </template>
      </el-table-column>
      <el-table-column  label="评论星级"  width="80"   prop="comment_star" >
         <template slot-scope="scope" > {{scope.row.comment_star}}星  </template>
      </el-table-column>

      <el-table-column  label="评论排序" width="80"  prop="comment_sort" ></el-table-column>
      <el-table-column  label="创建时间" width="200"  prop="create_time" > </el-table-column>
      <el-table-column  label="创建人"   width="200"   prop="create_name" ></el-table-column>
      <el-table-column  label="操作" fixed="right"  width="112" class-name="options">
        <template slot-scope="scope" >
          <i class="iconfont  icon-edit_normal"  v-if="$_has('/product_comment/edit')" @click.stop.prevent="setCurRow(scope.row, 'edit')"></i>  
          <i class="iconfont  icon-delete_normal" v-if="$_has('/product_comment/del')" @click.stop.prevent="setCurRow(scope.row, 'del')" ></i>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination ref="pagination" background :page-sizes="pageSizes" :total="total"  :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"  @size-change="handleSizeChange" :current-page="page"
      @current-change="handleCurrentChange"></el-pagination>
   
    <Add v-show="addShow" :show="addShow" @closeDialog="closeAddDialog"></Add>
    <Edit v-show="editShow" :id="curId" :show="editShow" @closeDialog="closeEditDialog"></Edit>
    <Del v-show="delShow" :idList="curList" :show="delShow" @closeDialog="closeDelDialog"></Del>
    <Export v-show="exportShow" :show="exportShow"  @closeDialog="closeExportDialog"></Export>
    <Sort v-show="sortShow" :show="sortShow" :idList="curList" @closeDialog="closeSortDialog"></Sort>

    
  </div>
</template>

<script>
import { Add, Del, Export, Edit, Sort } from './components'
import { productCommentList } from '@/api/product/productComment.js'
import { setSearchParams } from '@/utils/index.js'
import {  siteSiteList } from '@/api/manage/site.js'
import { secondTypeList, firstTypeList } from '@/api/product/productType.js'
import { productModelNameList, erpProducNameList } from '@/api/product/productBasic.js'
export default {
  name: 'product_comment',
  data() {
    return {
      searchForm: {
        product_model_name: { value: '', op: 'like' },
        site: { value: '',op: '=' },
        product_name: { value: '', op: 'like' },
        asin: { value: '',op: 'like' },
        first_type: { value: '',op: '=' },
        second_type: { value: '', op: '=' },
        comment_username: { value: '', op: 'like' },
        comment_time: { value: '', op: 'RANGE' },
        comment_star: {value: '', op: '='},
        comment_sort: {value: '', op: '='},
        create_name: {value: '', op: 'like'},
      },
      listTable: [  ],
      pageSizes: [20, 50, 100],
      total: 10,
      pageSize: 20,
      page: 1,

      curList: [],   
      curId: null,
      tableH: null,
      curIdList: [],
      selectList: [],
      loading: false,
      isExpand: false,

      commentTimePicker: '',
      commentTime: '',
      // 下拉框
      secondTypeList: [],
      firstTypeList: [],
      productModelNameList: [],
      siteList: [], //站点下拉框
      productNameList: [],
      commentStarList: [
        {value: '5', label: '5星'},
        {value: '4', label: '4星'},
        {value: '3', label: '3星'},
        {value: '2', label: '2星'},
        {value: '1', label: '1星'},
      ],
      commentSortList: [
        {value: '1', label: 'S'},
        {value: '2', label: 'A'},
        {value: '3', label: 'B'},
        {value: '4', label: 'C'},
        {value: '5', label: 'D'},
      ],
      addShow: false,
      delShow: false,
      exportShow: false,
      sortShow: false,
      editShow: false,
     }
  },
  watch:{
    commentTimePicker:function(n){
      if(n==null){
        this.commentTime = ""
      }
    },
  },
  created(){
    this.getTableList()
  },
  components: { Add, Del, Export, Edit, Sort },
  mounted(){
    let that = this;
    that.setTableHeight()
    // 注：window.onresize只能在项目内触发1次
    window.onresize = function windowResize () {
        // 通过捕获系统的onresize事件触发我们需要执行的事件
      that.setTableHeight()
    }
  },
  methods: {
    clearFirstType (){
      this.searchForm.second_type.value =""
      this.secondTypeList = []
    },
    getFirstTypeList(){// 一级类型
      firstTypeList().then(res=>{
        this.firstTypeList = res.data
      })
      this.searchForm.second_type.value = ""
    },
    getSecondTypeList(){ // 二级类型
      let params = {
        first_type: this.searchForm.first_type.value
      }
      if(params.first_type){
        secondTypeList(params).then(res=>{
          this.secondTypeList = res.data
        })
      }else{
        this.$message.error('请先选择一级类型')
      }
    },
  
    changeCommentTime (arr) {
      if(arr){
        let str = arr[0] + ' - ' + arr[1]
        this.commentTime = arr[0].split(' ')[0] +' - '+arr[1].split(' ')[0]
        this.searchForm.comment_time.value = str
      }else{
        this.searchForm.comment_time.value = null
      }
      this.getTableList()
    },
    isExpandForm () {
     
      this.isExpand = !this.isExpand
      if(this.isExpand ) {
        this.tableH = parseInt(this.tableH) - 43 + 'px'
      }else {
        this.tableH = parseInt(this.tableH) + 43 + 'px'
      }

    },
    RowClick (row) {
      this.$refs.table.toggleRowSelection(row);
    } ,
    setTableHeight(){
      let searchFormH = this.$refs.form.$el.offsetHeight;
      let manageMenuH = this.$refs.content.offsetHeight;
      let btnContainerH = this.$refs.btn.offsetHeight;
      let paginationH = this.$refs.pagination.$el.offsetHeight
      let tableHeight = manageMenuH  - btnContainerH - searchFormH - paginationH - 48 - 24 - 24
      this.tableH = tableHeight +'px' 
    },
    sureCloseDialog(type){
      if(this.curList.length > 0){
        if(type == 'sort'){
          this.closeSortDialog()
        } else if( type == 'del'){
          this.closeDelDialog()
        }
      } else {
        let word = "排序"
        if( type == 'del'){
          word = '删除'
        }
        this.$message.error('请选择需' + word + '的选项！')
      }
      
    },
    getTableList(){
      this.loading = true
      let params = setSearchParams(this.searchForm, this.pageSize, this.page)
      productCommentList(params).then(res=>{
        let list = res.data.data
        if( list.length == 0 && this.page > 1){
          this.page -= 1
          this.getTableList()
        }
        let idList = []
        for(let i = 0; i < list.length; i++){
          if(list[i].status == 1) {
            list[i].statusBoolean = true
          } else {
            list[i].statusBoolean = false
          }
          if(list[i].image){
            list[i].httpImage = this.utils.setCdnUrl()+list[i].image
          }
          idList.push(list[i].id)
        }
       
        this.curIdList = idList
        this.listTable = list
        this.total = res.data.total
        this.page = res.data.current_page
        this.loading = false
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableList()
     
    },
    handleCurrentChange(val) {
      this.page = val
      this.getTableList()
      this.$refs.table.clearSelection()
    },
    setCurRow(row, type) {
      this.curList = []
      this.curList.push(row.id)
      this.curId = row.id
      if(type == 'edit'){ 
        this.closeEditDialog()  
      }else if(type == 'del'){
        this.closeDelDialog()
      }
      
    },
    rowClassName({row}){
      let rowClass = "tableRowClass"
       if(row.statusSelection){
        rowClass += ' selectRow'
      }
      return rowClass
    },
    handleSelectionChange(list){
      let array = []
      for(let i = 0; i < list.length; i++ ){
        array.push(list[i].id)
      }
      this.curList = array

      // 选中行的样式赋值
      for(let i in this.selectList){
        this.selectList[i].statusSelection = false
      }
      for (let item in list){
        list[item].statusSelection = true
      }
      this.selectList = list
    },
     // 重置按钮
    resetForm(){
      for(let item in this.searchForm){
        this.searchForm[item].value =""
      }
      this.commentTime = ""
      this.commentTimePicker = ''
      this.getTableList()
    },
    
    
    //<!-- 查询条件下拉框 -->
    getSiteList(){ //站点
      siteSiteList().then(res=>{
        this.siteList = res.data
      })
    },
    getProductNameList (){
      erpProducNameList().then(res=>{
        if(res.state == 1){
          this.productNameList = res.data
        }
      })
    },
    getProductModelNameList () {
      productModelNameList().then(res=>{
        if(res.state == 1){
          this.productModelNameList = res.data
        }
      })
    },


    // <!-- 按钮弹窗控制 -->
    closeAddDialog () {
      this.addShow = !this.addShow
      if(!this.addShow){
        this.getTableList()
      }
    },
    closeDelDialog(){
      this.delShow = !this.delShow
      if(!this.delShow){
        this.getTableList()
      }
    },
    closeExportDialog(){
      this.exportShow = !this.exportShow
      if(!this.exportShow){
        this.getTableList()
      }
    },
    closeSortDialog () {
      this.sortShow = !this.sortShow
      if(!this.sortShow){
        this.getTableList()
      }
    },
    closeEditDialog () {
      this.editShow = !this.editShow
      if(!this.editShow){
        this.getTableList()
      }
    },
    
  },

}
</script>

<style lang="scss">
@import '@/styles/product/productComment.scss';

</style>